<?php

# http://projects:8081/cdapp-hole-api/api/send_post.php

include_once "_portal.php";
include_once "ip_query.php";

$ip_info = ip_query();
$ip = $ip_info['ip'];
$location = json_stringify([$ip_info['ccountry'], $ip_info['region'], $ip_info['city']]);

$forward = isset($post->forward) ? $post->forward : -1;
$text = isset($post->text) ? $post->text : "";
$office = isset($post->office) ? $post->office : 0;
$user = isset($post->user) ? json_stringify($post->user) : "null";
$mentioned = isset($post->mentioned) ? json_stringify($post->mentioned) : "[]";
$anonymous = isset($post->anonymous) ? $post->anonymous : 1;
$public = isset($post->public) ? $post->public : 1;

// $is_update = isset($post->isUpdate) ? $post->isUpdate : false;

$sql = "INSERT INTO `3hole_posts`(`forward`, `text`, `office`, `ip`, `location`, `user`, `mentioned`, `anonymous`, `public`) VALUES ({$forward}, '{$text}', {$office}, '{$ip}', '{$location}', '{$user}', '{$mentioned}',{$anonymous}, {$public})";
$result = exec_sql($sql);

$datetime = date('Y-m-d H:i:s');
$sql2 = "UPDATE `3hole_posts` SET `updated`='{$datetime}' WHERE `code`='{$forward}'";
$result2 = $forward > -1 ? exec_sql($sql2) : null;

output(['result' => $result, 'sql' => $sql, 'result2' => $result2, 'sql2' => $sql2]);